﻿<html>
<head>
	<title>HTML转换为base64 -- 清洁工2010版</title>
</head>
<body>
<table style="width:100%;height:100%;">
	<tr style="height:10%;">
		<td style="width:100px;">URL</td>
		<td>
			<input id="text_url" type="text" style="width:70%;" value="http://www.baidu.com/img/baidu_sylogo1.gif"/>
			<input type="button" value="Html生成base64" onclick="buttonHtmlBase64_Click()"/>
		</td>
	</tr>
	<tr style="height:20%;">
		<td>data</td><td><textarea id="textarea_data" style="width:100%; height:100%" /></textarea></td>
	</tr>
	<tr style="height:50%;">
		<td>BASE64</td><td><textarea id="textarea_base64" style="width:100%; height:100%" /></textarea></td>
	</tr>
	<tr style="height:20%;">
		<td>ResponseHeaders</td><td><textarea id="textarea_responseHeaders" style="width:100%; height:100%" /></textarea></td>
	</tr>
<script>
function $(id) { return document.getElementById(id); }

function requestHttp(url, type, data, cookie, loaded){
	if (typeof loaded != "function") return;
	var xmlhttp = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
	type = typeof type == "string" ? type : "POST";
	xmlhttp.open(type, url, false);
	if (cookie) xmlhttp.setRequestHeader("Cookie", cookie);
	xmlhttp.setRequestHeader("Referer", url); 
	xmlhttp.setRequestHeader("Accept-Encoding", "gzip,deflate"); 
	if (typeof data == "string") {
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		xmlhttp.setRequestHeader("Content-Length", data.length);
	}
	xmlhttp.send(data);
	loaded(xmlhttp);
}

function buttonHtmlBase64_Click() {
	requestHttp($("text_url").value, "GET", null, null, function(xmlhttp) {
		$("textarea_responseHeaders").value = xmlhttp.getAllResponseHeaders();
		var length = +xmlhttp.getResponseHeader("Content-Length");
		$("textarea_base64").value = bodyBase64(xmlhttp.responseBody);
		
		
		$("textarea_data").value = ['data:', xmlhttp.getResponseHeader("Content-Type"), ';base64,', 
			$("textarea_base64").value.replace(/\s+/g, "")
		].join("");
	});
}

function bodyBase64(body) {
	var doc = new ActiveXObject("MSXML2.DOMDocument");
	var node = doc.createElement("node");
	try {
		node.dataType = "bin.base64";
		node.nodeTypedValue = body;
		return node.text;
	} catch(e) {
		return e.message;
	} finally {
		node = null;
		doc = null;
	}
}
</script>
</body>
</html>